//
//  WBWelcomeViewController.swift
//  微博
//
//  Created by lipengfei on 15/9/6.
//  Copyright © 2015年 lipengfei. All rights reserved.
//

import UIKit

class WBWelcomeViewController: UIViewController {

    private var iconViewCons:NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        
    }
    override func viewDidAppear(animated: Bool) {
        //当视图显示之后加载动画
        //头像的动画，是由下面到上面
        // 通过使用自动布局进行动画， 原理： 1.先设置头像的初始位置 2. 再设置头像期望停留的位置 3.视图上更新布局
//        计算头像的应该停留的高度
        let h = -(UIScreen.mainScreen().bounds.height + iconViewCons!.constant)
        iconViewCons?.constant = h
        welcomeLabell.alpha = 0
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.8, animations: { () -> Void in
                    self.welcomeLabell.alpha = 1
                    }, completion: { (_) -> Void in
                        NSNotificationCenter.defaultCenter().postNotificationName(WBChangeRootViewControllerNotification, object: nil)
                })
        }
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    private func setupUI(){
        view.addSubview(iconView)
        view.addSubview(backgroundImage)
        view.addSubview(welcomeLabell)

        backgroundImage.ff_Fill(view)
        let cons = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 90, height: 90), offset:CGPoint(x: 0, y: -200))
        self.iconViewCons = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
        
        welcomeLabell.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 16))
    }
///  头像 属性
    private var iconView:UIImageView = {
        let icon = UIImageView(image: UIImage(named: "avatar_default_big"))
        icon.layer.cornerRadius = 45
        icon.layer.masksToBounds = true
        return icon
    }()
/// 背景图片
    private var backgroundImage:UIImageView =  UIImageView(image: UIImage(named: "ad_background"))
///  欢迎文字
    private var welcomeLabell:UILabel = UILabel(title: "欢迎归来", color: UIColor.darkGrayColor(), fontSize: 18)

}
